#!/bin/bash
clear
RED="\033[31m"    # Error message
GREEN="\033[32m"  # Success message
YELLOW="\033[33m" # Warning message
BLUE="\033[36m"   # Info message
RESET='\033[0m'

if [ "$EUID" -ne 0 ]; then
  echo -e "${RED} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

apt-get -qq update
apt-get -y -qq install stunnel4

cat >/etc/stunnel/stunnel.conf <<-EOF
cert = /etc/stunnel/stunnel.pem
client = no

socket = a:SO_REUSEADDR=1
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1

[dropbear service]
accept = 2021
connect = 127.0.0.1:339

[openvpn service]
accept = 2022
connect = 127.0.0.1:587

[shadowsocks-libev service]
accept = 2023
connect = 127.0.0.1:6561
EOF

openssl req -new -x509 -days 365 -nodes \
  -subj '/C=DO/ST=Dropbear/L=Debian/O="Cybertize"/OU="Cybertize Stunnel"/CN=cybertize.ml' \
  -out /etc/stunnel/stunnel.pem -keyout /etc/stunnel/stunnel.pem &>/dev/null
openssl dhparam 2048 >>/etc/stunnel/stunnel.pem

# /etc/default/stunnel
echo 'ENABLED=1
FILES="/etc/stunnel/*.conf"
OPTIONS=""
PPP_RESTART=0
RLIMITS="-n 4096"' >/etc/default/stunnel4
systemctl restart stunnel4

echo
echo -e "${GREEN} Pemasangan stunnel pakej telah selesai. ${RESET}"
echo
